package com.distributed.kv.entity;

import java.time.Instant;

import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;

/**
 * ID段实体类 - 修复字段映射问题
 * 添加@Column注解来正确映射数据库列名
 */
@Table("id_segments")
public class IdSegment {
    
    @PrimaryKey
    @Column("segment_key")  // 明确指定数据库列名
    private String segmentKey;
    
    @Column("current_id")
    private Long currentId;
    
    @Column("max_id")
    private Long maxId;
    
    @Column("step")
    private Integer step;
    
    @Column("updated_at")
    private Instant updatedAt;
    
    @Column("version")
    private Long version;
    
    // 默认构造函数
    public IdSegment() {}
    
    // 简化构造函数
    public IdSegment(String segmentKey, Integer step) {
        this.segmentKey = segmentKey;
        this.step = step;
        this.currentId = 0L;
        this.maxId = 0L;
        this.updatedAt = Instant.now();
        this.version = 0L;
    }
    
    // 全参构造函数
    public IdSegment(String segmentKey, Long currentId, Long maxId, Integer step, Instant updatedAt, Long version) {
        this.segmentKey = segmentKey;
        this.currentId = currentId;
        this.maxId = maxId;
        this.step = step;
        this.updatedAt = updatedAt;
        this.version = version;
    }
    
    // Getter和Setter方法
    public String getSegmentKey() {
        return segmentKey;
    }
    
    public void setSegmentKey(String segmentKey) {
        this.segmentKey = segmentKey;
    }
    
    public Long getCurrentId() {
        return currentId;
    }
    
    public void setCurrentId(Long currentId) {
        this.currentId = currentId;
    }
    
    public Long getMaxId() {
        return maxId;
    }
    
    public void setMaxId(Long maxId) {
        this.maxId = maxId;
    }
    
    public Integer getStep() {
        return step;
    }
    
    public void setStep(Integer step) {
        this.step = step;
    }
    
    public Instant getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(Instant updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public Long getVersion() {
        return version;
    }
    
    public void setVersion(Long version) {
        this.version = version;
    }
    
    /**
     * 检查当前段是否已耗尽
     */
    public boolean isExhausted() {
        return currentId != null && maxId != null && currentId >= maxId;
    }
    
    @Override
    public String toString() {
        return "IdSegment{" +
                "segmentKey='" + segmentKey + '\'' +
                ", currentId=" + currentId +
                ", maxId=" + maxId +
                ", step=" + step +
                ", updatedAt=" + updatedAt +
                ", version=" + version +
                '}';
    }
}